package day01;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author feng
 * @Date 2021/12/26 10:55
 * 字节输入流:读取指定字节byte[]数组   (从硬盘读取文件)
 * 读取时注意乱码
 * read() :  从输入流中读取数据的下一个字节，
 * read(byte[] b) :  从输入流中读取一定数量的字节
 */
public class FileInputStreamTest {
    public static void main(String[] args) {
        File file = new File("D:\\IO\\2.text");
        FileInputStream inputStream = null;
        int read = 0;
        byte[] b = new byte[5];
        try {
            inputStream = new FileInputStream(file);
            //读取单个字节，效率较低。
            while ((read = inputStream.read(b)) != -1) {
                System.out.print(new String(b, 0, read));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


}
